package com.jdd.pedicure.service;

import com.jdd.pedicure.dao.RoomDao;
import com.jdd.pedicure.entity.Result;
import com.jdd.pedicure.entity.Room;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class RoomService {
    @Resource
    RoomDao roomDao;

    public Room queryById(int id){
        return roomDao.getOne(id);
    }
    public Result<Room> queryAllfree(Result<Room> result){
        Example<Room> ex = Example.of(result.getExample());
        List<Room> all = roomDao.findAll(ex);
        result.setData(all);
        return result;
    }
    public void delete(int id){
        roomDao.deleteById(id);
    }
    public void saveOrUpdate(Room room){
        roomDao.save(room);
    }
    public Result<Room> queryByPage(Result<Room> pageBean) {
        PageRequest request = new PageRequest(pageBean.getPage() - 1, pageBean.getLimit());
        Example<Room> ex = Example.of(pageBean.getExample());
        Page<Room> all = roomDao.findAll(ex, request);
        pageBean.setData(all.getContent());
        pageBean.setCount((int) all.getTotalElements());
        return pageBean;
    }
}
